package cn.graydove.bot.listener;

import cn.graydove.bot.adapter.GroupMessageEvent;
import cn.graydove.bot.annotation.Listener;
import cn.graydove.bot.core.Assert;
import cn.graydove.bot.mirai.Client;
import net.mamoe.mirai.message.data.At;
import net.mamoe.mirai.message.data.Message;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.MessageChainBuilder;
import org.springframework.stereotype.Component;

import java.io.InputStream;

@Component
public class FunListener {

    private Assert asserts;

    public FunListener(Assert asserts) {
        this.asserts = asserts;
    }

    @Listener({".*爬.*", ".*@.*"})
    public void pa(GroupMessageEvent event, Client client) {
        asserts.groupReceive(event, (group)->{
            MessageChain message = client.getMessage();
            if (client.find("爬", message.contentToString()) != null) {
                MessageChainBuilder messages = new MessageChainBuilder();
                messages.add(new At(client.getSender()));
                messages.add(" 爬 ");
                client.sendMessage(messages);
                return;
            }
            for (Message msg : client.getMessage()) {
                if (msg instanceof At) {
                    if (((At) msg).getTarget() == client.getProperties().getBotQq()) {
                        MessageChainBuilder messages = new MessageChainBuilder();
                        messages.add(new At(client.getSender()));
                        messages.add(" 爬 ");
                        client.sendMessage(messages);
                    }
                }
            }
        });

    }

    @Listener("涩图")
    public void seImage(GroupMessageEvent event, Client client) {
        asserts.groupReceive(event, (a) -> {
            InputStream images = this.getClass().getResourceAsStream("/images/metoo.jpg");
            client.sendImage(images);
        });
    }

    @Listener("确实")
    public void truth(GroupMessageEvent event, Client client) {
        asserts.groupReceive(event, (a) -> {
            InputStream images = this.getClass().getResourceAsStream("/images/truth.png");
            client.sendImage(images);
        });
    }
}
